package com.antler.smt.portal.service.impl;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 备份数据库
 *
 * @author zhangyi
 * @date 2018/6/21 16:03
 */
@Service
@PropertySource("classpath:backupconfig.properties")
public class BackUpDataBaseServiceImpl {

    @Value("${hostIp}")
    private String hostIp;

    @Value("${mysql.user.name}")
    private String userName;

    @Value("${password}")
    private String password;

    @Value("${savePath}")
    private String savePath;

    @Value("${databaseName}")
    private String databaseName;

    /**
     * 备份数据库
     *
     * @return
     */
    public boolean backup() throws IOException, InterruptedException {
        //返回标记状态
        boolean flag = false;

        //数据库的ip地址
        String hostIP = this.getHostIp();
        //用户名
        String userName = this.getUserName();
        //密码
        String password = this.getPassword();
        //保存路径
        String savePath = this.getSavePath();
        //保存文件名
        String fileName = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date(System.currentTimeMillis())).toString()+".sql";
        //数据库名称
        String databaseName = this.getDatabaseName();
        File file = new File(savePath+fileName);
        file.createNewFile();
        //保存的文件夹
        File saveFile = new File(savePath);
        //创建备份文件夹
        if(!saveFile.exists()){
            saveFile.mkdirs();
        }
        //执行linux上的命令
        String execStr = " mysqldump -h" + hostIP + " -u" + userName + " -p" + password + " --set-charset=UTF8 " + databaseName;
        //创建执行命令
        Process process = Runtime.getRuntime().exec(execStr);
        //读取命名输出流
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream(),"utf-8"));
        //将流写入文件
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),"utf-8"));
        //带读取字符
        String tempStr = "";
        while((tempStr = bufferedReader.readLine()) != null){
            bufferedWriter.write(tempStr);
            bufferedWriter.write( System.getProperty("line.separator"));
        }
        bufferedReader.close();
        bufferedWriter.close();
        //表示正常执行完成
        if(process.waitFor() == 0){
            flag = true;
        }
        return flag;
    }


    public String getHostIp() {
        return hostIp;
    }

    public void setHostIp(String hostIp) {
        this.hostIp = hostIp;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getSavePath() {
        return savePath;
    }

    public void setSavePath(String savePath) {
        this.savePath = savePath;
    }

    public String getDatabaseName() {
        return databaseName;
    }

    public void setDatabaseName(String databaseName) {
        this.databaseName = databaseName;
    }
}
